﻿using CMS.Models;
using CMS.View;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CMS.ViewModel
{
    public class UnitTypeViewModel:ViewModelBase
    {
        /// <summary>
        /// 所有单位类型
        /// </summary>
        private List<UnitType> unitTypes = new List<UnitType>();
        public List<UnitType> UnitTypes
        {
            get { return unitTypes; }
            set { unitTypes = value; RaisePropertyChanged(nameof(UnitTypes)); }
        }
        public UnitTypeViewModel()
        {
            unitTypes = new UnitTypeProvider().Select();
        }
        /// <summary>
        /// 打开添加记录对话框
        /// </summary>
        public RelayCommand OpenAddUnitWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddUnitTypeWindow();
                    window.ShowDialog();
                    //刷新数据
                    UnitTypes = new UnitTypeProvider().Select();
                });
            }
        }
        /// <summary>
        /// 删除物资单位命令
        /// </summary>
        public RelayCommand<object> DeleteUnitCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is UnitType model)) return;
                    var cardgo = new CargoProvider().Select().FindAll(t => t.UnitId == model.Id);
                    if (cardgo != null && cardgo.Count > 0) { MessageBox.Show("当前类型已经有用"); return; }
                    var count = new UnitTypeProvider().Delete(model);
                    if (count > 0) MessageBox.Show("操作成功", "温馨提示");
                    //删除数据
                    UnitTypes = new UnitTypeProvider().Select();
                });
            }
        }
    }
}
